#!/bin/bash

set -o pipefail;

PGNAME=`basename $0`
PGDIRNAME=`dirname $0`
PGERR="$PGNAME[$$]: "


if [ "${MLPROD_HOME}" == "" ]
then
  echo "${PGERR} :  MLPROD_HOME env not set;  exiting !!!" >&2
  exit 10
fi

PGCFG_FILE="${MLPROD_HOME}/config/mlprog.rc"
if [ ! -f ${PGCFG_FILE} ]
then
  echo "${PGERR} : config file <${PGCFG_FILE}> not found; exiting !!!" >&2
  exit 10
fi

. "${PGCFG_FILE}"

CUST_CTX="$(mlget_cust_ctx 'cust')"
MOD_CTX="$(mlget_cust_ctx  'mod')"
BATCH_WFL_ID=1                  # workflow/product id
BATCH_MODE="oper"
if [ ! -z ${BATCH_RUN_MODE} ]
then
  BATCH_MODE="${BATCH_RUN_MODE}"
fi
LOG_CTX="${BATCH_MODE}-${CUST_CTX}"


PGCFG_FILE="${MLPROD_HOME}/cust/${CUST_CTX}/config/${MOD_CTX}.rc"
if [ ! -f ${PGCFG_FILE} ]
then
  echo "${PGERR} : config file <${PGCFG_FILE}> not found; exiting !!!" >&2
  exit 10
fi

. "${PGCFG_FILE}"
if [ ! -f ${PGCFG_FILE} ]
then
  echo "${PGERR} : config file sourcing <${PGCFG_FILE}> error; exiting !!!" >&2
  exit 10
fi


function cleanup
{
  rm -f ${EDER_DSTATUS_FILE};
  rm -f ${EDER_ESTATUS_FILE};

  if [ -f ${OUT_INSURANCE_FILE} ]
  then
    rm -f ${OUT_INSURANCE_FILE}
    if [ $? -ne 0 ]
    then
      mllogerr "[${LOG_CTX}] : ERR : Unable to cleanup files : ${OUT_INSURANCE_FILE}";
    fi
  fi

}

trap cleanup EXIT ERR TERM INT QUIT

function usage()
{
  mllogerr "Usage: $PGNAME batch_recv_date in MMDDYY_HHMMSS_XXXXXX format ";
}

if [ $# -lt 1 ]
then
  usage;
  exit 10;
else
  if [ "${2}" == "-s" ]
  then
    skip=1
  else
    skip=0
  fi
fi


# get the batch dates BATCH_RECV_DATE, BATCH_RUN_DATE, BATCH_EXEC_DATE
mlget_batch_dates "${1}"
if [ $? -ne 0 ] || [ -z "${BATCH_RECV_DATE}" ]
then
  usage
  exit 10
fi

# LOG
LOG_CTX="${LOG_CTX}-${BATCH_RECV_DATE}"


# set file locations
if [ ! -d ${BATCH_CTX_DIR} ]
then
  mkdir ${BATCH_CTX_DIR}
  if [ $? -ne 0 ]
  then
    mllogerr "[${LOG_CTX}] : ERR : unable to create batch dir : ${BATCH_CTX_DIR}"
    exit 10;
  fi
fi

# rcpt for encr
ERCPT_EMDEON="-r mkey_emdeon"

#GPG STATUS FILE
EDER_DSTATUS_FILE=${BATCH_CTX_DIR}/${BATCH_RECV_DATE}_ins.d
EDER_ESTATUS_FILE=${BATCH_CTX_DIR}/${BATCH_RECV_DATE}_ins.e
ENCR="${ENCR} -s ${EDER_ESTATUS_FILE}"
DECR="${DECR} -s ${EDER_DSTATUS_FILE}"


# BATCH INSURANCE1 FILE
IN_INSURANCE_FILE=${ARCH_CTX_BAK_DIR}/${BATCH_RECV_DATE}_batch_insurance0_t.gpg
OUT_INSURANCE_FILE=${BATCH_CTX_DIR}/${BATCH_RECV_DATE}_${BATCH_WFL_ID}_batch_insurance0_t.gpg

# EQUIFAX FILE WITH SSN
IN_EFX_FILE=${ARCH_CTX_IN_DIR}/${BATCH_RECV_DATE}_efx.gpg
OUT_EFX_FILE=${BATCH_CTX_DIR}/${BATCH_RECV_DATE}_${BATCH_WFL_ID}_efx.gpg

# BATCH EMDEON OUT FILE
EMDEON_OUT_FILE=${ARCH_CTX_OUT_DIR}/${CUST_CTX}_${BATCH_RECV_DATE}_emdeon.txt.pgp


if [ ! -r ${IN_INSURANCE_FILE} ]
then
  mllogerr "[${LOG_CTX}] : ERR : batch insurance1 file not found : ${IN_INSURANCE_FILE}";
  exit 10;
fi

if [ -f ${OUT_INSURANCE_FILE} ]
then
#  mllogerr "[${LOG_CTX}] : ERR : Converted batch insurance1 file exist in batch area : ${OUT_INSURANCE_FILE}";
#  exit 10;
   rm -f ${OUT_INSURANCE_FILE}
fi


TASK_ID=0
tstart=$((`date +%s`))

tstamp="00:00:00"
mllogerr "[${LOG_CTX}] : BEG : [${tstamp}] : PROCESS PART-1 START"


TASK_ID=`expr $TASK_ID + 1`
tmsg="load sql modules";
mllogerr "[${LOG_CTX}] : INF : TASK-${TASK_ID} $tmsg";
ts=$((`date +%s`))
${SQL_CTX_CMN_RUNT_DIR}/load_funcs.sh
if [ $? -ne 0 ]
then
  mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
  cleanup;
  exit 1$TASK_ID;
fi
te=$((`date +%s`))

ts=$((`date +%s`))
${SQL_CTX_RUNT_DIR}/load_funcs.sh
if [ $? -ne 0 ]
then
  mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
  cleanup;
  exit 1$TASK_ID;
fi
te=$((`date +%s`))


TASK_ID=`expr $TASK_ID + 1`
tmsg="update batch insurance1 table prior to scoring";
mllogerr "[${LOG_CTX}] : INF : TASK-${TASK_ID} $tmsg";
ts=$((`date +%s`))
${PGSQL} ${SQL_CTX_RUNT_DIR}/upd_table_batch_insurance1_prescoring.sql
if [ $? -ne 0 ]
then
  mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
  cleanup;
  exit 1$TASK_ID;
fi
te=$((`date +%s`))



####### THIS SECTION IS TO APPEND ZIP4

if [ "${ZIP4_FLAG}" == "YES" ] && [ $MMACHINE -eq 1 ]
then

   TASK_ID=`expr $TASK_ID + 1`
   tmsg="dump the seqnum and address into text file";
   mllogerr "[${LOG_CTX}] : INF : TASK-${TASK_ID} $tmsg";
   ts=$((`date +%s`))
   ${MLBIN_DIR}/extract_address_for_zip4_append_payorintel.pl -c "${CUST_CTX}" -m "${MOD_CTX}" -b "${BATCH_RECV_DATE}"\
        > ${MLPROD_HOME}/tmp/inbound_${CUST_CTX}_zip4_append_insurance1_${BATCH_RECV_DATE}.txt
   if [ $? -ne 0 ]
   then
     mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
     cleanup;
     exit 1$TASK_ID;
   fi
   te=$((`date +%s`))


   linenum=$(/usr/bin/wc -l ${MLPROD_HOME}/tmp/inbound_${CUST_CTX}_zip4_append_insurance1_${BATCH_RECV_DATE}.txt )
   linenum=${linenum%% *}

   if [ $linenum -eq 0 ]; then           #empty file

       mllogerr "[${LOG_CTX}] : END : temp output file to run through QAS Process was empty - skipped";
       /bin/rm -f ${MLPROD_HOME}/tmp/inbound_${CUST_CTX}_zip4_append_insurance1_${BATCH_RECV_DATE}.txt; 

   else 

   	TASK_ID=`expr $TASK_ID + 1`
   	tmsg="running C program to append zip4 and dum results into text file";
   	mllogerr "[${LOG_CTX}] : INF : TASK-${TASK_ID} $tmsg";
   	ts=$((`date +%s`))
   	${MLBIN_DIR}/running_zip4.sh ${MLPROD_HOME}/tmp/inbound_${CUST_CTX}_zip4_append_insurance1_${BATCH_RECV_DATE}.txt \
                                     ${MLPROD_HOME}/tmp/outbound_${CUST_CTX}_zip4_append_insurance1_${BATCH_RECV_DATE}.txt
   	if [ $? -ne 0 ]
   	then
      	  mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
          cleanup;
          exit 1$TASK_ID;
        fi
        te=$((`date +%s`))


        TASK_ID=`expr $TASK_ID + 1`
        tmsg="load results into batch_zip4_insurance1_t";
        mllogerr "[${LOG_CTX}] : INF : TASK-${TASK_ID} $tmsg";
        ts=$((`date +%s`))
        cat ${MLPROD_HOME}/tmp/outbound_${CUST_CTX}_zip4_append_insurance1_${BATCH_RECV_DATE}.txt \
        | ${PGSQL} ${SQL_CTX_RUNT_DIR}/load_table_batch_zip4_insurance1.sql
        if [ $? -ne 0 ]
        then
          mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
          cleanup;
          exit 1$TASK_ID;
        fi
        te=$((`date +%s`))
        if [ -f ${MLPROD_HOME}/tmp/inbound_${CUST_CTX}_zip4_append_insurance1_${BATCH_RECV_DATE}.txt ]; then 
          rm -f ${MLPROD_HOME}/tmp/inbound_${CUST_CTX}_zip4_append_insurance1_${BATCH_RECV_DATE}.txt;
        fi
   
        if [ -f ${MLPROD_HOME}/tmp/outbound_${CUST_CTX}_zip4_append_insurance1_${BATCH_RECV_DATE}.txt ]; then
          rm -f ${MLPROD_HOME}/tmp/outbound_${CUST_CTX}_zip4_append_insurance1_${BATCH_RECV_DATE}.txt
        fi


        TASK_ID=`expr $TASK_ID + 1`
        tmsg="merge with batch_insurance1_t to append zip4 to that table";
        mllogerr "[${LOG_CTX}] : INF : TASK-${TASK_ID} $tmsg";
        ts=$((`date +%s`))
        ${PGSQL} ${SQL_CTX_RUNT_DIR}/upd_table_batch_insurance1_zip4.sql
        if [ $? -ne 0 ]
        then
          mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
          cleanup;
          exit 1$TASK_ID;
        fi
        te=$((`date +%s`))

   fi

fi

######### END OF ZIP4 APPEND SECTION


TASK_ID=`expr $TASK_ID + 1`
tmsg="calculate the score";
mllogerr "[${LOG_CTX}] : INF : TASK-${TASK_ID} $tmsg";
ts=$((`date +%s`))
${PGSQL} ${SQL_CTX_RUNT_DIR}/calculate_score.sql
if [ $? -ne 0 ]
then
  mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
  cleanup;
  exit 1$TASK_ID;
fi
te=$((`date +%s`))


TASK_ID=`expr $TASK_ID + 1`
tmsg="load score into batch insurance1 table";
mllogerr "[${LOG_CTX}] : INF : TASK-${TASK_ID} $tmsg";
ts=$((`date +%s`))
${PGSQL} ${SQL_CTX_RUNT_DIR}/upd_table_batch_insurance1_postscoring.sql
if [ $? -ne 0 ]
then
  mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
  cleanup;
  exit 1$TASK_ID;
fi
te=$((`date +%s`))


TASK_ID=`expr $TASK_ID + 1`
tmsg="loading the emdeon table";
mllogerr "[${LOG_CTX}] : INF : TASK-${TASK_ID} $tmsg";
ts=$((`date +%s`))
${PGSQL} ${SQL_CTX_RUNT_DIR}/load_table_emdeon_out.sql
if [ $? -ne 0 ]
then
  mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
  cleanup;
  exit 1$TASK_ID;
fi
te=$((`date +%s`))


TASK_ID=`expr $TASK_ID + 1`
tmsg="copy batch emdeon out table";
mllogerr "[${LOG_CTX}] : INF : TASK-${TASK_ID} $tmsg";
ts=$((`date +%s`))
${PGSQL} ${SQL_CTX_BAK_DIR}/copy_table_emdeon_out.sql \
  | ${ENCR} ${ERCPT_EMDEON} -f - > ${EMDEON_OUT_FILE}
if [ $? -ne 0 ]
then
  mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
  cleanup;
  exit 1$TASK_ID;
fi
te=$((`date +%s`))



TASK_ID=`expr $TASK_ID + 1`
tmsg="loading the insurance1_sent_detail table";
mllogerr "[${LOG_CTX}] : INF : TASK-${TASK_ID} $tmsg";
ts=$((`date +%s`))
${PGSQL} ${SQL_CTX_RUNT_DIR}/load_table_insurance1_sent_detail.sql
if [ $? -ne 0 ]
then
  mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
  cleanup;
  exit 1$TASK_ID;
fi
te=$((`date +%s`))



TASK_ID=`expr $TASK_ID + 1`
tmsg="loading the insurance1 table";
mllogerr "[${LOG_CTX}] : INF : TASK-${TASK_ID} $tmsg";
ts=$((`date +%s`))
${PGSQL} ${SQL_CTX_RUNT_DIR}/load_table_insurance1.sql
if [ $? -ne 0 ]
then
  mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
  cleanup;
  exit 1$TASK_ID;
fi
te=$((`date +%s`))


#TASK_ID=`expr $TASK_ID + 1`
#tmsg="inserting new records into insurance1_npi table";
#mllogerr "[${LOG_CTX}] : INF : TASK-${TASK_ID} $tmsg";
#ts=$((`date +%s`))
#${PGSQL} ${SQL_CTX_RUNT_DIR}/load_table_insurance1_npi.sql
#if [ $? -ne 0 ]
#then
#  mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
#  cleanup;
#  exit 1$TASK_ID;
#fi
#te=$((`date +%s`))


TASK_ID=`expr $TASK_ID + 1`
tmsg="inserting new records into insurance1_prior table";
mllogerr "[${LOG_CTX}] : INF : TASK-${TASK_ID} $tmsg";
ts=$((`date +%s`))
${PGSQL} ${SQL_CTX_RUNT_DIR}/load_table_insurance1_prior.sql
if [ $? -ne 0 ]
then
  mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
  cleanup;
  exit 1$TASK_ID;
fi
te=$((`date +%s`))



tstamp="$(mltstamp ${tstart})"

mllogerr "[${LOG_CTX}] : END : [${tstamp}] : PROCESS PART-1 DONE"

exit 0;
